﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblCust_MarketDAL:ItblCust_Market
    {
        HKEA_CMSEntities model;
        tblCust_Market market;

        public tblCust_MarketDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblCust_MarketDAL GetInstance()
        {
            return new tblCust_MarketDAL();
        }

        public List<tblCust_Market> GetListByENT(tblCust_Market ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblCust_Market.Where(a => a.CustMarketUID > 0);
            if (ent.CustMarketUID > 0)
                result = result.Where(a => a.CustMarketUID == ent.CustMarketUID);
            if (ent.CountryUID > 0)
                result = result.Where(a => a.CountryUID == ent.CountryUID);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.Deleted >= 0)
                result = result.Where(a => a.Deleted == ent.Deleted);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<tblCust_Market> GetListByENT(tblCust_Market ent)
        {
            var result = model.tblCust_Market.Where(a => a.CustMarketUID > 0);
            if (ent.CustMarketUID > 0)
                result = result.Where(a => a.CustMarketUID == ent.CustMarketUID);
            if (ent.CountryUID > 0)
                result = result.Where(a => a.CountryUID == ent.CountryUID);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.Deleted >= 0)
                result = result.Where(a => a.Deleted == ent.Deleted);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(tblCust_Market ent)
        {
            var result = model.tblCust_Market.Where(a => a.CustMarketUID > 0);
            if (ent.CustMarketUID > 0)
                result = result.Where(a => a.CustMarketUID == ent.CustMarketUID);
            if (ent.CountryUID > 0)
                result = result.Where(a => a.CountryUID == ent.CountryUID);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.Deleted >= 0)
                result = result.Where(a => a.Deleted == ent.Deleted);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int InsertENT(tblCust_Market ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblCust_Market.AddObject(ent);
            model.SaveChanges();
            ent = model.tblCust_Market.Where(a => a.CustMarketUID > 0).OrderBy(a => a.CustMarketUID).ToList().Last();
            return ent.CustMarketUID;
        }

        /// <summary>
        /// 修改一条记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int UpdateENT(tblCust_Market ent)
        {
            market = model.tblCust_Market.Where(a=>a.CustMarketUID==ent.CustMarketUID).FirstOrDefault();
            if (market != null)
            {
                market.Deleted = ent.Deleted;
                market.LastModifyBy = ent.LastModifyBy;
                market.LastModifyDate = DateTime.Now;
                market.CountryUID = ent.CountryUID;
                market.CustomerUID = ent.CustomerUID;
                market.Remarks = ent.Remarks;
            }
            return model.SaveChanges();
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int DeleteENT(tblCust_Market ent)
        {
            var temp = model.tblCust_Market.Where(a => a.CustMarketUID > 0);
            if (ent.CustMarketUID > 0)
                temp = temp.Where(a=>a.CustMarketUID==ent.CustMarketUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblCust_Market.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
